# Silence superfluous messages
options(warn = -1)
options(dplyr.summarise.inform = FALSE)

# Resolve function conflicts
select <- dplyr::select
alpha <- psych::alpha

# Generate custom skim function
percent_skim <- skim_with(
    factor = sfl(percents = ~ {
        # Generate percentage table
        percent_table <- round(prop.table(table(.)) * 100, 1)

        # Clean up values
        values <- sprintf("%.1f%%", percent_table)

        # Bind names with values and return
        paste(
            names(percent_table), values,
            sep = ": ", collapse = "; "
        )
    }),
    character = sfl(percents = ~ {
        # Generate percentage table
        percent_table <- round(prop.table(table(.)) * 100, 1)

        # Clean up values
        values <- sprintf("%.1f%%", percent_table)

        # Bind names with values and return
        paste(
            names(percent_table), values,
            sep = ": ", collapse = "; "
        )
    }),
)

# Write functions to summarize distributions
dist_mean <- function(x) {
    if (is.na(x)) {
        return(NA)
    }

    click_dist <- x %>%
        str_split(",") %>%
        unlist()
    score_dist <- c()

    for (i in 0:10) {
        score_dist <- c(score_dist, rep(i, click_dist[i + 1]))
    }

    return(mean(score_dist))
}

dist_sd <- function(x) {
    if (is.na(x)) {
        return(NA)
    }

    click_dist <- x %>%
        str_split(",") %>%
        unlist()
    score_dist <- c()

    for (i in 0:10) {
        score_dist <- c(score_dist, rep(i, click_dist[i + 1]))
    }

    return(sd(score_dist))
}
